package com.mojo.concurrent.threadsafe;

/**
 * 线程yield方法使用示例
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2021, 广州九尾信息科技有限公司
 * @date 2021/8/19 9:52
 */
public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        MyThread1 t1 = new MyThread1();
        Thread t = new Thread(t1);
        t.start();
        System.out.println(Thread.interrupted());
        //底层t调用wait方法，会释放t对象上的同步锁。
        //此时当前线程锁不会释放，且进入waiting或timed_waiting状态
        //线程t执行完或者join时间到，jvm底层线程调用notify方法，当前线程就进入就绪状态
        t.join();
        System.out.println("主线程。。。。");
    }

    static class MyThread1 implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(3000);
                Thread t2 = new Thread(new MyThread2());
                t2.start();
                t2.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("MyThread1====>" + Thread.currentThread().getName());
        }
    }

    static class MyThread2 implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("MyThread2===>" + Thread.currentThread().getName());
        }
    }

}
